package com.nf.health.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.customview.SwipeView.OnSwipeStatusChangeListener;
import com.nf.health.app.models.HistoryVo;

import java.util.ArrayList;
import java.util.List;

public class HistoryDataAdapter extends BaseAdapter {
	
	String[] Str;
	
	private ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();
	
	private Context context;
	private List<HistoryVo> doctors;



	public HistoryDataAdapter(Context context, List<HistoryVo> result) {
		this.context = context;
		this.doctors = result;
	}

	@Override
	public int getCount() {
		if (doctors != null) {
			return doctors.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return doctors.get(position);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	@Override
	public View getView(final int position, View view, ViewGroup viewGroup) {
		final ViewHolder holder;
		if (view == null) {
			view = View.inflate(context, R.layout.my_history_data_item, null);
			holder = new ViewHolder();
			holder.history_rq = (TextView) view.findViewById(R.id.history_time);
			holder.history_bs = (TextView) view.findViewById(R.id.history_bs);
			holder.history_jl = (TextView) view.findViewById(R.id.history_jl);
			holder.history_sm = (TextView) view.findViewById(R.id.history_sm);
			holder.history_kll = (TextView) view.findViewById(R.id.history_kll);
			holder.swipeView  = (SwipeView) view.findViewById(R.id.swipeView);
			holder.delete  = view.findViewById(R.id.delete);
			holder.line  = view.findViewById(R.id.history_line);
			view.setTag(holder);
			
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener != null)
					listener.onClick(position, "");		
				unClosedSwipeView.clear();
			}
		});
		holder.swipeView.setOnSwipeStatusChangeListener(new OnSwipeStatusChangeListener() {

			@Override
			public void onSwiping(SwipeView swipingSwipeView) {
				if (!unClosedSwipeView.contains(swipingSwipeView)) {
					closeAllOpenedSwipeView();
				}

				if (!unClosedSwipeView.contains(swipingSwipeView)) {
					unClosedSwipeView.add(swipingSwipeView);
				}

			}

			@Override
			public void onOpen(SwipeView openedSwipeView) {
				for (int i = 0; i < unClosedSwipeView.size(); i++) {
					if (unClosedSwipeView.get(i) != openedSwipeView) {
						unClosedSwipeView.get(i).close();
					}
				}
				if (!unClosedSwipeView.contains(openedSwipeView)) {
					unClosedSwipeView.add(openedSwipeView);
				}
			}

			@Override
			public void onClose(SwipeView closedSwipeView) {
				unClosedSwipeView.remove(closedSwipeView);
			}
		});
		
		String value = doctors.get(position).getPosttime();
		
		Str = value.split(" ");
		
		holder.history_rq.setText(Str[0]);
		
		
		holder.history_bs.setText(doctors.get(position).getSetnum());
		holder.history_jl.setText(doctors.get(position).getKilometer());
		double sleepDuration = Double.parseDouble(TextUtils.isEmpty(doctors.get(position).getSleep()) ? "0.0":doctors.get(position).getSleep());
		holder.history_kll.setText(doctors.get(position).getCalorie());

		if(sleepDuration == 0){
			holder.history_sm.setText(String.format("%.2f", sleepDuration));
			holder.line.setVisibility(View.GONE);
		}else{
			holder.line.setVisibility(View.VISIBLE);
			holder.history_sm.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if(listener != null)
						listener.onClick(position, "sm");
				}
			});
			holder.history_sm.setText(String.format("%.2f", sleepDuration));
		}

		return view;
	}

	class ViewHolder {
		TextView history_rq, history_bs, history_jl,history_sm,history_kll;
		SwipeView swipeView;
		View delete;
		View line;
	}
	
	private OnMyClickListener listener;
	public void setOnMyClickListener(OnMyClickListener listener){

		this.listener = listener;
	}
	public void setMyDatanotifyChanged(List<HistoryVo> result){
		this.doctors = result;
		notifyDataSetChanged();
	}
	private void closeAllOpenedSwipeView() {
        for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }
    }
	
}
